package org.jeecg.modules.app.v1.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.jeecg.modules.app.v1.shop.entity.ApiBdEntity;
import org.jeecg.modules.app.v1.shop.mapper.ApiSysUserMapper;
import org.jeecg.modules.app.v1.shop.service.ApiShopConfigService;
import org.jeecg.modules.app.v1.shop.service.ApiShopService;
import org.jeecg.modules.app.v1.util.Const;
import org.jeecg.modules.power.config.service.IYlCheckConfigService;
import org.jeecg.modules.power.group.entity.GroupUser;
import org.jeecg.modules.power.group.service.IYlGroupService;
import org.jeecg.modules.power.shop.entity.YlShop;
import org.jeecg.modules.power.shop.entity.YlTransferShopRecords;
import org.jeecg.modules.power.shop.service.IYlShopService;
import org.jeecg.modules.power.shop.service.IYlTransferShopRecordsService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.*;

/**
 * 这里是一个注释
 *
 * @author scw
 * @version v1.0
 * @date
 */
@Service
@RequiredArgsConstructor
public class ApiShopServiceImpl implements ApiShopService {

    private final ApiSysUserMapper apiSysUserMapper;
    private final IYlShopService shopService;
    private final ISysUserService sysUserService;
    private final IYlTransferShopRecordsService transferShopRecordsService;
    private final IYlCheckConfigService configService;
    private final IYlGroupService ylGroupService;

    /**
     * 获取系统中的BD列表
     * @param name
     * @return
     */
    public List<ApiBdEntity> getBdList(String name){
       return apiSysUserMapper.getBdList(name);
    }


    /**
     * 提交门店转移操作
     * @param id 接收者（BD）ID
     * @param shopId 门店ID
     * @param reason 转移原因
     */
    @Transactional
    public void transfer(String id,String shopId,String reason){

        SysUser sysUser = sysUserService.getById(id);
        String receiveName = String.format("%s|%s",sysUser.getRealname(),sysUser.getPhone());
        Date date = new Date();
        List<String> shopIds = Arrays.asList(shopId.split(","));
        List<YlTransferShopRecords> recordsList = new ArrayList<>();

        /**
         * check 1 需要审核2直接通过不审核
         */
        Integer check = configService.getCheckTransferShop()==1?2:1; //1自动审核2手动审核

        for(String sId:shopIds){
            YlTransferShopRecords records = new YlTransferShopRecords();
            records.setReceiveId(id);
            records.setTransferReason(reason);
            YlShop shop = shopService.getById(sId);
            records.setShopId(sId);
            SysUser transfer = sysUserService.getById(shop.getBdId());
            String transferName = String.format("%s|%s",transfer.getRealname(),transfer.getPhone());
            records.setReceiveName(receiveName);
            records.setTransferName(transferName);
            records.setTransferId(shop.getBdId());
            records.setTransferTime(date);
            records.setCreateTime(date);
            records.setCreateBy(transferName);
            records.setTransferStatus(check);
            /**
             * 如果自动审核，直接修改门店信息
             */
            if(Objects.equals(check, Const.NO_2))transferShop(sId,sysUser);
            else records.setIsCheck(Const.isCheck_1); //记录当时是自动审核通过的
            recordsList.add(records);
        }
        transferShopRecordsService.saveBatch(recordsList);
    }

    /**
     * 确认接收
     * @param shopId 门店ID
     */
    @Transactional
    public void transferShop(String shopId,SysUser sysUser){
        GroupUser groupUser = ylGroupService.getUserGroup(sysUser.getId());
        String shopBd = String.format("%s|%s",sysUser.getRealname(),sysUser.getPhone());
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.set("bd_id",groupUser.getUserId());
        wrapper.set("shop_bd",shopBd);
        wrapper.set("org_id",groupUser.getGroupId());
        wrapper.set("claim_time",new Date());
        wrapper.eq("id",shopId);
        shopService.update(wrapper);
    }

    /**
     * 确认接收
     * @param id 转移记录ID
     * @param flag 同意或拒绝标记  1同意2拒绝
     * @param reason 拒绝原因
     */
    @Transactional
    public void confirm(String id,Integer flag,String reason){
        YlTransferShopRecords records = transferShopRecordsService.getById(id);
        if(flag==1){
            YlShop shop = shopService.getById(records.getShopId());
            shop.setShopBd(records.getReceiveId());
            shopService.updateById(shop);
        }else if(flag ==2){
            records.setTransferStatus(3);
            records.setTransferReason(reason);
            transferShopRecordsService.updateById(records);
        }
    }

}
